<?php

declare(strict_types=1);

namespace SimpleSAML\Test\Module\core\Storage;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Configuration;
use SimpleSAML\Error\CriticalConfigurationError;
use SimpleSAML\Module\core\Storage\SQLPermanentStorage;

use function sleep;
use function sys_get_temp_dir;
use function unlink;

/**
 * Test for the SQLPermanentStorage class.
 */
#[CoversClass(SQLPermanentStorage::class)]
class SQLPermanentStorageTest extends TestCase
{
    /** @var \SimpleSAML\Module\core\Storage\SQLPermanentStorage */
    private static SQLPermanentStorage $sql;


    /**
     * @throws \Exception
     */
    public static function setUpBeforeClass(): void
    {
        // Create instance
        $config = Configuration::loadFromArray([
            'datadir' => sys_get_temp_dir(),
        ]);
        self::$sql = new SQLPermanentStorage('test', $config);
    }


    /**
     */
    public static function tearDownAfterClass(): void
    {
        @unlink(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'sqllite' . DIRECTORY_SEPARATOR . 'test.sqlite');
    }


    /**
     *
     * @throws \Exception
     */
    public function testMissingDatadirThrowsException(): void
    {
        $config = Configuration::loadFromArray([]);

        $this->expectException(CriticalConfigurationError::class);
        $this->expectExceptionMessage("The configuration is invalid: Missing 'datadir' in configuration.");
        new SQLPermanentStorage('test', $config);
    }


    /**
     */
    public function testSet(): void
    {
        // Set a new value
        self::$sql->set('testtype', 'testkey1', 'testkey2', 'testvalue', 2);

        // Test getCondition
        /** @var array $result */
        $result = self::$sql->get();
        $this->assertEquals('testvalue', $result['value']);
    }


    /**
     */
    public function testSetOverwrite(): void
    {
        // Overwrite existing value
        self::$sql->set('testtype', 'testkey1', 'testkey2', 'testvaluemodified', 2);

        // Test that the value was actually overwritten
        $result = self::$sql->getValue('testtype', 'testkey1', 'testkey2');
        $this->assertEquals('testvaluemodified', $result);

        /** @var array $result */
        $result = self::$sql->getList('testtype', 'testkey1', 'testkey2');
        $this->assertEquals('testvaluemodified', $result[0]['value']);
    }


    /**
     */
    public function testNonexistentKey(): void
    {
        // Test that getting some non-existing key will return null / empty array
        $result = self::$sql->getValue('testtype_nonexistent', 'testkey1_nonexistent', 'testkey2_nonexistent');
        $this->assertNull($result);
        $result = self::$sql->getList('testtype_nonexistent', 'testkey1_nonexistent', 'testkey2_nonexistent');
        $this->assertEmpty($result);
        $result = self::$sql->get('testtype_nonexistent', 'testkey1_nonexistent', 'testkey2_nonexistent');
        $this->assertNull($result);
    }


    /**
     */
    public function testExpiration(): void
    {
        // Make sure the earlier created entry has expired now
        sleep(3);

        // Make sure we can't get the expired entry anymore
        $result = self::$sql->getValue('testtype', 'testkey1', 'testkey2');
        $this->assertNull($result);

        // Now add a second entry that never expires
        self::$sql->set('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring', 'testvalue_nonexpiring', null);

        // Expire entries and verify that only the second one is still there
        self::$sql->removeExpired();
        $result = self::$sql->getValue('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring');
        $this->assertEquals('testvalue_nonexpiring', $result);
    }


    /**
     */
    public function testRemove(): void
    {
        // Now remove the nonexpiring entry and make sure it's gone
        self::$sql->remove('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring');
        $result = self::$sql->getValue('testtype', 'testkey1_nonexpiring', 'testkey2_nonexpiring');
        $this->assertNull($result);
    }
}
